package LuceneIndexCreate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import LuceneIndexCreate.model.CatIdsFrom1688;
import LuceneIndexCreate.model.CatIndexData;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryparser.classic.ParseException;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.*;

/**
 * Created by User on 2017/12/11.
 */
public class TestCorrectRate {

    public static void main(String[] args) throws IOException, InterruptedException, ParseException {

        File file = new File("E:\\lucene\\TestData.txt");

        if (!file.exists()) return;

        FileInputStream input = new FileInputStream(file);
        byte[] bs = new byte[1024];
        int len = 0;
        StringBuilder sb = new StringBuilder();
        while ((len = input.read(bs)) > 0) {
            sb.append(new String(bs, 0, len));
        }

        JSONArray list = JSON.parseArray(sb.toString());

        if (list == null) return;

        FileOutputStream correctFile = new FileOutputStream("E:\\lucene\\测试10\\correct.txt");
        int correctCount = 0;
        int totalCount = list.size();
        FileOutputStream incorrectFile = new FileOutputStream("E:\\lucene\\测试10\\incorrect.txt");
        int doneCount = 0;
        for (Object object : list) {
            System.out.println(doneCount++);
            JSONObject map = (JSONObject) object;
            String correctCid = map.getString("cid");
            String numiid = map.getString("numiid");
            CatIdsFrom1688 item = new CatIdsFrom1688("", "", Arrays.asList(numiid));
            CatIndexData data = GetDesc.getDate(item);

            String desc = data.getDesc();
            if (StringUtils.isEmpty(desc)) {
                System.out.println("未获取宝贝keyword");
                totalCount--;
                continue;
            }

            List<Document> documents = LuceneIndex.searchDocument(desc);


            List searchCids = new ArrayList();
            for (int i = 0; i < 5 && i < documents.size(); i++) {
                Document document = documents.get(i);
                String searchCid = document.get("cid");
                searchCids.add(searchCid);
            }
            int matchPotion = searchCids.indexOf(correctCid);
            Boolean isCorrect = matchPotion >= 0;


            Map m = new LinkedHashMap();
            m.put("isCorrect", isCorrect);
            m.put("correctCid", correctCid);
            m.put("numiid", numiid);
            m.put("searchKeyword", desc);
            m.put("matchPotion", matchPotion);
            m.put("searchResult", searchCids);
            if (isCorrect) {
                // 正确
                correctCount++;
                correctFile.write(JSON.toJSONString(m).getBytes());
                correctFile.write("\r\n".getBytes());
                correctFile.flush();
            } else {
                // 错误
                incorrectFile.write(JSON.toJSONString(m).getBytes());
                incorrectFile.write("\r\n".getBytes());
                incorrectFile.flush();
            }
        }

        System.out.println(correctCount + "/" + totalCount);
        System.out.println(correctCount * 1d / totalCount);

        correctFile.close();
        incorrectFile.close();

    }


}
